/**
 * http 下载请求
 */
export const httpDownload = function (http, url, query, fileName) {
  $("#spinner").show();
  http.get(getSchoolFrom().fileServerDomain + url, {params: query, responseType: 'blob'}).then(function (ret) {
    $("#spinner").hide();
    let url = window.URL.createObjectURL(new Blob([ret.data]));
    let link = document.createElement('a');
    link.style.display = 'none';
    link.href = url;
    link.setAttribute('download', fileName);

    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    window.URL.revokeObjectURL(url);
  }, function (err) {
    Message.error('请求出现了错误，请联系系统管理员！');
    // toastr.error('请求出现了错误，请联系系统管理员！', '请求异常');
  });
}

/**
 * 取浏览器信息
 */
export const getBrowserInfo = function () {
  let Sys = {};
  let explorer = window.navigator.userAgent.toLowerCase();
  let compare = function (s) {
    return (explorer.indexOf(s) >= 0);
  };
  let ie11 = (function () {
    return ("ActiveXObject" in window);
  })();
  let ver = (function (bro) {
    var tmp = explorer.split(bro);
    var verArr = tmp[1].split(' ');
    return verArr[0];
  });
  if (compare("msie") || ie11) {
    Sys = {browser: 'ie', ver: '11.0'};
    return Sys;
  } else if (compare("firefox") && !ie11) {
    Sys = {browser: 'firefox', ver: ver('firefox/')};
    return Sys;
  } else if (compare("qqbrowser") && !ie11) {
    Sys = {browser: 'qqbrowser', ver: ver('qqbrowser/')};
    return Sys;
  } else if (compare("opr") && !ie11) {
    Sys = {browser: 'opera', ver: ver('opr/')};
    return Sys;
  } else if (compare("chrome") && !ie11) {
    if (explorer.indexOf("edge") > -1) {
      Sys = {browser: 'edge', ver: ver('edge/')};
      return Sys;
    } else {
      Sys = {browser: 'chrome', ver: ver('chrome/')};
      return Sys;
    }
  } else if (compare("safari") && !ie11) {
    Sys = {browser: 'safari', ver: ver('safari/')};
    return Sys;
  } else if (compare("micromessenger") && !ie11) {
    Sys = {browser: 'weixin', ver: ''};
    return Sys;
  } else {
    Sys = {browser: '未知', ver: ''};
    return Sys
  }
};

// 浏览文件
export const browseFile = function (url, fileToken) {
  var suffix = url.split('.').pop();
  if (suffix === 'jpg' || suffix === 'jpeg' || suffix === 'png' || suffix === 'bmp' || suffix === 'gif' || suffix === 'webp') {
    return url + '?fileToken=' + encodeURIComponent(fileToken);
  } else if (suffix === 'mp4' || suffix === 'ogv' || suffix === 'mp3' || suffix === 'wav') {
    return url + '?fileToken=' + encodeURIComponent(fileToken);
  } else if (suffix === 'doc' || suffix === 'docx') {
    return 'https://view.officeapps.live.com/op/view.aspx?src=' + encodeURIComponent(url + '?fileToken=' + encodeURIComponent(fileToken));
  } else if (suffix === 'ppt' || suffix === 'pptx') {
    return 'https://view.officeapps.live.com/op/view.aspx?src=' + encodeURIComponent(url + '?fileToken=' + encodeURIComponent(fileToken));
  } else if (suffix === 'pdf') {
    return 'http://mozilla.github.io/pdf.js/web/viewer.html?file=' + encodeURIComponent(url + '?fileToken=' + encodeURIComponent(fileToken));
  } else {
    return url + '?fileToken=' + encodeURIComponent(fileToken);
  }
};

// 下划线转换驼峰
export const underlineToHump = function (name) {
  return name.replace(/\_(\w)/g, function (all, letter) {
    return letter.toUpperCase();
  });
};

// 驼峰转换下划线
export const humpToUnderline = function (name) {
  var newName = name.replace(/([A-Z])/g, "_$1").toLowerCase();
  return newName;
};

/**
 * 获取光标位置
 * obj: 光标dom节点
 */
export const getCursortPosition = (obj) => {
  var cursorIndex = 0;
  if (document.selection) {
      // IE Support
      obj.focus();
      var range = document.selection.createRange();
      range.moveStart('character', -obj.value.length);
      cursorIndex = range.text.length;
  } else if (obj.selectionStart || obj.selectionStart==0) {
      // another support
      cursorIndex = obj.selectionStart;
  }
  return cursorIndex;
}

/**
 * 设置光标位置
 * obj: 光标dom节点
 * pos: 位置索引
 */
export const setCursorPosition = (obj, pos) => {
  if(obj.setSelectionRange)
  {
      obj.focus();
      obj.setSelectionRange(pos,pos);
  }
  else if (obj.createTextRange) {
      var range = obj.createTextRange();
      range.collapse(true);
      range.moveEnd('character', pos);
      range.moveStart('character', pos);
      range.select();
  }
}

